1 module hip.concurrency.mutex;
2 import hip.config.opts;
3 
4 
5 
6 
7 
8 static if(HipConcurrency)
9 class DebugMutex
10 {
11     import core.sync.mutex : Mutex;
12     import core.thread;
13     
14     private string lastFileLock;
15     private size_t lastLineLock;
16     private ThreadID lastID;
17 
18     private string lastFileUnlock;
19     private size_t lastLineUnlock;
20 
21     private Mutex mtx;
22 
23     private ThreadID mainThreadId;
24 
25     this(ThreadID mainId = ThreadID.init)
26     {
27         this.mainThreadId = mainId;
28         mtx = new Mutex();
29     }
30     void lock(string file = __FILE__, size_t line = __LINE__)
31     {
32         import hip.concurrency.internal;
33         if(lastLineLock == 0)
34         {
35             lastLineUnlock = 0;
36             lastFileUnlock = null;
37 
38             lastFileLock = file;
39             lastLineLock = line;
40             lastID = thisThreadID;
41         }
42         else
43         {
44             version(Desktop)
45             {
46                 import hip.console.log;
47                 import hip.util.conv:to;
48                 string last = (lastID == mainThreadId ? "Main " : "") ~ "Thread("~to!string(lastID)~")";
49                 string curr = (thisThreadID == mainThreadId ? "Main " : "") ~ "Thread("~to!string(thisThreadID)~")";
50 
51 
52                 logln("Tried to lock a locked mutex at ", file, ":", line,
53                 "\n\tLast locked at ", lastFileLock, ":",lastLineLock, " ", last, 
54                 " Current Thread is ",curr);
55             }
56         }
57         mtx.lock();
58     }
59     void unlock(string file = __FILE__, size_t line = __LINE__)
60     {
61         version(Desktop)
62         {
63             if(lastLineLock == 0)
64             {
65                 import hip.concurrency.internal;
66                 import hip.console.log;
67                 import hip.util.conv:to;
68                 string last = (lastID == mainThreadId ? "Main " : "") ~ "Thread("~to!string(lastID)~")";
69                 string curr = (thisThreadID == mainThreadId ? "Main " : "") ~ "Thread("~to!string(thisThreadID)~")";
70                 
71                 logln(
72                     "Tried to unlock an unlocked mutex at ", file, ":", line, 
73                     "\n\tLast unlocked at ",  lastFileUnlock, ":",lastLineUnlock, " ", last,
74                     " Current Thread is ",curr
75                 );
76                 // throw new Error("Tried to unlock an unlocked mutex");
77             }
78         }
79         lastLineUnlock = line;
80         lastFileUnlock = file;
81         lastFileLock = null;
82         lastLineLock = 0;
83         mtx.unlock();
84     }
85 
86 }
87 else
88 class DebugMutex
89 {
90     this(ulong id = 0){}
91     final void lock(){}
92     final void unlock(){}
93 }